<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/19 14:16
 * Remark:
 */

namespace App\Http\Controllers\Contract;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Contract\AddContractFormatTempPost;
use App\Http\Requests\Contract\ListContractTempGet;
use App\Service\Contract\ContractFormatTemplateService;

class ContractFormatTemplateController extends Controller
{
    private ContractFormatTemplateService $_contractFormatTempService;

    public function __construct()
    {
        $this->middleware([
            'contractFormatTempLeg'
        ]);
        $this->_contractFormatTempService = new ContractFormatTemplateService();
    }


    /**
     * @param AddContractFormatTempPost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addContractFormatTemplate(AddContractFormatTempPost $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();
        $res =  $this->_contractFormatTempService->addContractFormatTemplate($data);
        return ResponseFunc::returnJson(
            'ok',
            [
                "contract_format_template_id" =>$res
            ],
            '新增格式合同模板成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }


    /**
     * 修改合同类型
     * @param int $contractFormatTemplateId
     * @return \Illuminate\Http\JsonResponse
     */
    public function editContractFormatTemplate(int $contractFormatTemplateId): \Illuminate\Http\JsonResponse
    {
        $data = request()->post();
        $data['contract_format_template_id'] = $contractFormatTemplateId;
        $this->_contractFormatTempService->editContractFormatTemplate($data);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '修改格式合同模板成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 删除合同类型
     * @param int $contractFormatTemplateId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteContractFormatTemplate(int $contractFormatTemplateId): \Illuminate\Http\JsonResponse
    {
        $this->_contractFormatTempService->deleteContractFormatTemplate($contractFormatTemplateId);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除格式合同模板成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 获取合同详情
     * @param int $contractFormatTemplateId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractFormatTemplateInfo(int $contractFormatTemplateId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractFormatTempService->getContractFormatTemplateInfo($contractFormatTemplateId),
            '获取格式合同模板详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 获取合同类型下拉
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractFormatTemplateSelect(): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractFormatTempService->getContractFormatTemplateSelect(),
            '获取格式合同模板下拉成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 获取格式合同模板列表
     * @param ListContractTempGet $request
     * @return mixed
     */
    public function getContractFormatTemplateList(ListContractTempGet $request): mixed
    {
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractFormatTempService->getContractFormatTemplateList($pageSize,$filters),
            '获取格式合同模板列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function setFormatDocx(int $contractFormatTemplateId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractFormatTempService->formatDocx($contractFormatTemplateId),
            '替换格式合同模板成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function setClientEvaluationTemplate(): \Illuminate\Http\JsonResponse
    {
        $data = request()->all();
        $fileName = $data['client_name']."-".$data['client_type_name']."-客户信用评价表";
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractFormatTempService->formatClientEvaluationTemplate($fileName,$data['template_data_json_str']),
            '生成客户信用评价表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

}